package com.wanban.tools

import android.content.Context
import android.content.SharedPreferences
import com.wanban.base.MyApplication
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class SPUtil {

    companion object {
        private const val KEY_TOKEN: String = "key_token"
        private const val KEY_TOKEN_CREATE_TIME: String = "token_create_time"
        const val KEY_NOTIFY_DIALOG_ID: String = "notify_dialog_id"
        const val KEY_NOTIFY_SYSTEM: String = "notify_system_id"
        private const val KEY_UMENG_INIT = "uminit"
        val instance: SPUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            SPUtil()
        }
    }



    /** token */
    var token: String by PreferenceDelegate(KEY_TOKEN, "")
    /** token过期时间 */
    var tokenExpireTime: Long by PreferenceDelegate(KEY_TOKEN_CREATE_TIME, 0L)
    /** 是否允许使用4G播放视频 */
    var isAllowPlayVideoBy4G: Boolean by PreferenceDelegate("isAccessUserMobilisAllowPlayVideoBy4GePlayVideo", false)
    /** 转换密码（后续废弃） */
    var tf: String by PreferenceDelegate("transfrom", "false")
    /** 夜间模式切换 默认日间模式 */
    var isStyleNight: Boolean by PreferenceDelegate("isStyleNight", true)
    /** 是否需要升级 */
    var isNeedUpdate: Boolean by PreferenceDelegate("isUpdatable", false)

    /**  */
    var downloadCount: Int by PreferenceDelegate("download_count", 10)

    var isAllowPosition: Boolean by PreferenceDelegate("ISALLOWPOSITION", true)

    var notifySystemId: Int  by PreferenceDelegate(KEY_NOTIFY_SYSTEM, 0)

    var utid: String by PreferenceDelegate("uitd", "")
    var utidCreateTime: Long by PreferenceDelegate("utidCreateTime", 0L)

    var isNotDisplaySSL: Boolean by PreferenceDelegate("isNotDisplaySSL", false)
    // 是否已经定位过 0: 未定位过; 1:受限 ;2:不受限
    var locationStatus: Int by PreferenceDelegate("locationStatus", 0)

    // 当前线上版本号
    var netAppVer: Int by PreferenceDelegate("net_app_last_version", 0)

    var playerDecoder: Boolean by PreferenceDelegate("playerDecoder", false)

    var isNodePage: Boolean by PreferenceDelegate("isNodePage", true)




    fun clear() {
        PreferenceDelegate("", 0L).clear()
    }

    @Suppress("UNCHECKED_CAST")
    class PreferenceDelegate<T>(private val propName: String, private val defaultValue: T) : ReadWriteProperty<Any, T> {

        private val sharedPreferences: SharedPreferences by lazy {
            MyApplication.context.getSharedPreferences(
                "day_video_sp",
                Context.MODE_PRIVATE
            )
        }

        fun clear() {
            sharedPreferences.edit().clear().apply()
        }

        override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
            value?.let { putSPValue(propName, value) }
        }

        override fun getValue(thisRef: Any, property: KProperty<*>): T {
            return getSPValue(propName, defaultValue) ?: defaultValue
        }

        private fun <T> getSPValue(name: String, defaultValue: T): T? = with(sharedPreferences) {
            val result = when (defaultValue) {
                is String -> getString(name, defaultValue)
                is Int -> getInt(name, defaultValue)
                is Long -> getLong(name, defaultValue)
                is Float -> getFloat(name, defaultValue)
                is Boolean -> getBoolean(name, defaultValue)
                else -> null
            }
            result as T
        }

        private fun <T> putSPValue(name: String, value: T) = with(sharedPreferences.edit()) {
            when (value) {
                is Long -> putLong(name, value)
                is String -> putString(name, value)
                is Int -> putInt(name, value)
                is Boolean -> putBoolean(name, value)
                is Float -> putFloat(name, value)
                else -> null
            }
        }?.apply()
    }

}

